const app = getApp()
const {target} = app.globalData 
import verifyToken from "../utils/verifyToken"
import request from "../api/request"

// 保存用户信息到服务端
export const addUserInfo = (userInfo)=>{
  console.log(userInfo);
  let ret = new Promise((resolve,reject)=>{
    wx.request({
      url: target + '/userinfo',
      method:"POST",
      data:userInfo,
      success(res){
        resolve(res.data)
      },
      fail(err){
        reject(err)
      }
    })
  })
  
  return ret  
}

// 获取用户信息
export const getUserInfo = async ()=>{
  // 获取手机号
  try {
    let {phoneNumber} = await verifyToken()

    let ret = await request({
      url:"/userinfo",
      method:"GET",
      data:{
        phoneNumber
      }
    })

    // console.log(ret)
    if(ret.code === 200){
      return ret.userInfo
    }
  } catch (error) {
    console.log(error)
    return Promise.reject(error)
  }
}

// 修改用户生日
export const updateUserBirthday = async (birthday)=>{
  try {
    let {phoneNumber} = await verifyToken()

    let ret = await request({
      url:"/userbirthday",
      method:"POST",
      data:{
        phoneNumber,
        birthday
      }
    })

    return ret
  } catch (error) {
    console.log(error);
  }
}

// 上传家人信息
export const updateFamilyInfo = async (obj) =>{
  try {
    let {phoneNumber} = await verifyToken()

    let ret = request({
      url:"/familyinfo",
      method:"POST",
      data:{
        phoneNumber,
        familyInfo:obj
      }
    })

    return ret
  } catch (error) {
    console.log(error);
  }
}

// 删除聊天室id
export const deleteChatRoomId = async (userId,chatRoomId)=>{
  let ret = await request({
    url:"/userInfo/delete/chatRoomId",
    method:"POST",
    data:{
      userId,
      chatRoomId
    }
  })

  if (ret.code == 201) {
    return ret.msg
  }
}
